package io.renren.modules.live.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.rabbitmq.client.Channel;
import io.renren.common.utils.OrderCodeUtil;
import io.renren.modules.live.entity.FanweOrderEntity;
import io.renren.modules.live.entity.FanweVideoPropEntity;
import io.renren.modules.live.vo.AnchorVo;
import io.renren.modules.live.vo.UserAnchorUpdateInfoVo;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.live.dao.FanweAnchorDao;
import io.renren.modules.live.entity.FanweAnchorEntity;
import io.renren.modules.live.service.FanweAnchorService;

import javax.annotation.Resource;

@Log4j2
@Service("fanweAnchorService")
public class FanweAnchorServiceImpl extends ServiceImpl<FanweAnchorDao, FanweAnchorEntity> implements FanweAnchorService {




    @Resource
    private FanweAnchorDao fanweAnchorDao;



    /**
     * 监听队列主播增加礼物收入
     * @throws IOException
     */
    @RabbitListener(queues = "exchange.anchor.bonus")
    public void consume(Message message, Channel channel) throws IOException {
        //FanweVideoPropEntity propEntity = (FanweVideoPropEntity) rabbitTemplate.receiveAndConvert("exchange.anchor.bonus");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);//手动确认
        byte[] body = message.getBody();
        String msgJson = new String(body,"UTF-8");
        JSONObject jsonObject1 = JSONObject.parseObject(msgJson);
        Long anchorId = jsonObject1.getLong("anchorId");
        BigDecimal propMoney = jsonObject1.getBigDecimal("propMoney");

        log.info("收到消息: {}", message.toString());
        //数据库主播增加礼物收入金额
        try {
            fanweAnchorDao.updateAnchorMoney(anchorId,propMoney);
            System.out.println("主播礼物金额增加成功");
        }catch (Exception e){
            log.info(e);
            System.out.println("主播礼物金额增加失败");
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FanweAnchorEntity> page = this.page(
                new Query<FanweAnchorEntity>().getPage(params),
                new QueryWrapper<FanweAnchorEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public FanweAnchorEntity queryByUserName(String nickNumber) {
        return fanweAnchorDao.queryByUserName(nickNumber);
    }

    @Override
    public List<Long> findByVoideListVideoNameAndAnchorName(String content) {
        return fanweAnchorDao.findByVoideListVideoNameAndAnchorName(content);
    }

    @Override
    public void addAnchorFile(Long anchorId, String fileUrl) {
        fanweAnchorDao.addAnchorFile(anchorId,fileUrl);
    }

    @Override
    public void updateUserById(UserAnchorUpdateInfoVo userAnchorUpdateInfoVo) {
        fanweAnchorDao.updateUserById(userAnchorUpdateInfoVo);
    }

    /**
     * 管理员查看主播列表
     * @param anchorVo
     * @return
     */
    @Override
    public Map<String, Object> selectAnchorList(AnchorVo anchorVo) throws Exception{
        Map<String, Object> map=new HashMap<>();
        anchorVo.setPageIndex((anchorVo.getPageIndex()-1)*anchorVo.getPageSize());
        List<FanweAnchorEntity> fanweAnchorEntities = fanweAnchorDao.selectAnchorList(anchorVo);
        Integer integer = fanweAnchorDao.selectAnchorListCount(anchorVo);
        map.put("fanweAnchorEntities",fanweAnchorEntities);
        map.put("count",integer);
        return map;
    }

    /**
     * 新建主播账号
     * @param fanweAnchorEntity
     * @return
     */
    @Override
    public FanweAnchorEntity insertAnchor(FanweAnchorEntity fanweAnchorEntity) throws Exception{
        fanweAnchorEntity.setAnchorId(IdWorker.getId());
        fanweAnchorEntity.setUserPwd(DigestUtils.sha256Hex(fanweAnchorEntity.getUserPwd()));
        fanweAnchorEntity.setCode(UUID.randomUUID().toString().replace("-",""));
        Integer integer = fanweAnchorDao.insertAnchor(fanweAnchorEntity);
        return fanweAnchorEntity;
    }

    /**
     * 新建主播账号
     * @return
     * @throws Exception
     */
    @Override
    public String obtainNickNumber() throws Exception {
        Integer integer=-1;
        String nickNumber=null;
        while (integer<=0){
            int i = (int) ((Math.random() * 9 + 1) * 100000000);
            nickNumber=String.valueOf(i);
            AnchorVo anchorVo=new AnchorVo();
            anchorVo.setNickNumberInsert(nickNumber);
            integer = fanweAnchorDao.selectAnchorListCount(anchorVo);
        }
        return nickNumber;
    }
    /**
     * 管理员查看主播信息
     * @param anchorId
     * @return
     */
    @Override
    public FanweAnchorEntity selectAnchorInformation(String anchorId) {
        FanweAnchorEntity fanweAnchorEntity = fanweAnchorDao.selectAnchorInformation(anchorId);
        return fanweAnchorEntity;
    }

    /**
     * 修改主播信息状态
     * @param anchorId
     * @param status
     * @return
     */
    @Override
    public Integer updateAnchorInStatus(String anchorId, String status) {
        Integer integer = fanweAnchorDao.updateAnchorInStatus(anchorId, status);
        return integer;
    }

}